package kz.vokebuz.demo.db;

import kz.vokebuz.demo.SmsMmsMsg;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class DbAdapter  extends ContentProvider{

	private static final String TABLE_NAME = "smslog";
	public final String COLUMN_TIMESTAMP = "time";
	public final String COLUMN_SENDER = "sender";
	public final String COLUMN_TEXT = "msgtext";
	
	private SQLiteDatabase db;
	private static DbOpenHelper mDB;
	private Context context;
    private ContentValues values ;

	public DbAdapter(Context context) {
		this.context = context;
		values = new ContentValues();
	}

    public void open(){
    	mDB = new DbOpenHelper(context);
    	db = mDB.getWritableDatabase();
    }
	public long addRow(SmsMmsMsg msg) {
		if (values.size()>0) {
			values.clear();
		}
		values.put(COLUMN_TIMESTAMP, msg.getTimeStamp());
		values.put(COLUMN_SENDER, msg.getMsgOriginator());
		values.put(COLUMN_TEXT, msg.getMessageBody());

		// ask the database object to insert the new data
		 
		 return	db.insert(TABLE_NAME, null, values);

	}


	public int countRecs() throws SQLException {
		Cursor cur;
		cur = db.rawQuery("select COUNT(*) from smslog", null);
		return cur.getInt(0);
		
	}
    
	public Cursor getAllSms(){
		return db.rawQuery("select * from smslog", null);
		
	}
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		mDB = new DbOpenHelper(context);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}
}
